onEvent('player.logged_in', event => {
  event.server.scheduleInTicks(1, event.server, function (callback) {
    // 获取玩家和世界对象 
    if (!event.player.stages.has('first')) {
      let player = event.player;
      event.player.stages.add('first');
      player.tell('Welcome, you can use');
      player.tell(text.red('/ftbteamislands home'));
      player.tell('return your skyblock');
      player.tell('You`ll only see this conversation the first time you enter the game');
      player.tell('');
      player.tell('欢迎您,您可以使用');
      player.tell(text.red('/ftbteamislands home'));
      player.tell('来回到您的空岛');
      player.tell('你只会在第一次进入游戏看到这段对话');


    }
    if (event.player.persistentData.playerpoll) {
      console.log('Player ' + event.player.name + ' logged in. Reading data...');
      //恒定数值
      event.player.data['poll'] = parseInt(event.player.persistentData.playerpoll);
      event.player.data['sutpoll'] = parseInt(event.player.persistentData.sutpoll);
      event.player.tell('radiation : ' + event.player.data['poll'] / 100 + ' mSv');

      event.player.data['theplayeronline'] = 1;
      event.player.persistentData.theplayeronline = 1;
    }
    else {
      event.player.data['poll'] = 0;
      event.player.persistentData.playerpoll = 0;
      event.player.data['sutpoll'] = 100
      event.player.persistentData.sutpoll = 100;

      event.player.data['theplayeronline'] = 1;
      event.player.persistentData.theplayeronline = 1;
    }

    event.server.schedule(0.1 * MINUTE, event.server, function (callback) {

      if (event.player.persistentData.theplayeronline == 1 && event.player.data['theplayeronline'] == 1) {
        //console.log("玩家在线，继续执行!");
        setPoll(event, event.player.data['sutpoll']);
        if (event.player.data['poll'] == 1000000) {
          event.player.kill();
        }
        callback.reschedule();
      }
      else {
        console.log("线程1释放");
      }
    });

    event.server.schedule(1 * MINUTE, event.server, function (callback) {
      if (event.player.persistentData.theplayeronline == 1 && event.player.data['theplayeronline'] == 1) {
        //console.log("玩家在线，继续执行!");
        var player = event.player;
        var count = player.data['poll'];
        giveEffects(event, count);
        setSutpoll(event, 0 - Math.floor(event.player.data['sutpoll'] * 0.01));
        callback.reschedule();
      }
      else {
        console.log("线程2释放");
      }

    });



  })

});


onEvent('player.logged_out', event => {
  console.log('Player ' + event.player.name + ' logged out. Saving data...');
  event.player.persistentData.playerpoll = event.player.data['poll'];
  event.player.persistentData.sutpoll = event.player.data['sutpoll'];

  event.player.data['theplayeronline'] = 0;
  event.player.persistentData.theplayeronline = 0;
});




function setPoll(event, x) {
  let poll = event.player.data['poll'];
  var player = event.player;
  event.player.data['poll'] = setPolls(player, x, poll);
}

function setSutpoll(event, x) {
  let poll = event.player.data['sutpoll'];
  var player = event.player;
  event.player.data['sutpoll'] = setSutPolls(player, x, poll);
}

//我一年前写的代码这么这么屎.......
function setPolls(player, x, poll) {
  //获取玩家的药水效果
  let rpoll = poll;
  let potionEffects = player.getPotionEffects();
  if(potionEffects.isActive("huahuamagicconverter:complete_resistance")){
    ///effect give fafaava huahuamagicconverter:complete_resistance
    rpoll = 0;
  }
  else if(potionEffects.isActive("huahuamagicconverter:radiation_resistance")){
    ///effect give fafaava huahuamagicconverter:radiation_resistance
    rpoll = parseInt(poll + (x * 0.3));
  }
  else if (potionEffects.isActive("minecraft:regeneration")) {
    rpoll = parseInt(poll + (x * 0.6));
  }
  else {
    rpoll = poll + x;
  }

  if (rpoll < 1) {
    rpoll = 1;
  }
  else if (rpoll > 1000000) {
    rpoll = 1000000;
  }
  return rpoll;
}



function setSutPolls(player, x, poll) {
  //获取玩家的药水效果
  let rpoll = poll;
  let potionEffects = player.getPotionEffects();
  if(potionEffects.isActive("huahuamagicconverter:complete_resistance")){
    ///effect give fafaava huahuamagicconverter:complete_resistance
    rpoll = 0;
  }
  else if(potionEffects.isActive("huahuamagicconverter:radiation_resistance")){
    ///effect give fafaava huahuamagicconverter:radiation_resistance
    rpoll = parseInt(poll + (x * 0.3));
  }
  else if (potionEffects.isActive("minecraft:regeneration")) {
    rpoll = parseInt(poll + (x * 0.6));
  }
  else {
    rpoll = poll + x;
  }

  if (rpoll < 1) {
    rpoll = 1;
  }
  else if (rpoll > 1000000) {
    rpoll = 1000000;
  }
  return rpoll;
}



function giveEffects(event, count) {
  const Effect = [
    'minecraft:unluck', //霉运
    'minecraft:weakness', //虚弱
    'minecraft:slowness', //缓慢
    'minecraft:mining_fatigue', //挖掘疲劳
    'minecraft:hunger', //饥饿
    'minecraft:poison', //中毒
  ]
  var player = event.player;
  if (count < 100000)
    return;
  //event.server.runCommandSilent(`/effect give ${player.name} ${i.effect} ${i.time} ${i.level}`);
  const stages = 100000;
  let buffCount = Math.floor(count / stages);
  let x = 0;
  let buffLevel = Math.floor(buffCount / 2) - 1 < 0 ? 0 : Math.floor(buffCount / 2) - 1;
  buffLevel = buffLevel >= 4 ? 4 : buffLevel;
  //console.log(`buffCount = ${buffCount} , buffLeavel = ${buffLevel}`);
  Effect.forEach((i) => {
    //console.log(x);
    if (x < buffCount) {
      event.server.runCommandSilent(`/effect give ${player.name} ${i} ${60} ${buffLevel}`);
      //console.log(`/effect give ${player.name} ${i} ${600} ${Math.floor(buffLevel)}`);
      x++;

    }

  })
}